package com.zhouchb.blog.service.impl;

import com.zhouchb.blog.bean.Comment;
import com.zhouchb.blog.dao.CommentRepository;
import com.zhouchb.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Zhouchb
 * @Create 2021-09-2021/9/12 :22:17
 * @Description
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository commentRepository;

    @Override
    public List<Comment> listCommentByBlogIdService(Long blogId) {
        List<Comment> comments = commentRepository.listCommentByBlogId(blogId);
       // return comments;
        return eachComment(comments,blogId);
    }

    @Override
    public Comment getCommentByIdService(Long parentCommentId) {
        return commentRepository.getCommentById(parentCommentId);
    }

    @Transactional
    @Override
    public int saveCommentService(Comment comment) {
        Long parentCommentId = comment.getParentComment().getId();
        if (parentCommentId != -1) {
            comment.setParentComment(commentRepository.getCommentById(parentCommentId));
        } else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentRepository.saveComment(comment);
    }

    /**
     * 循环每个顶级的评论节点
     */
    private List<Comment> eachComment(List<Comment> comments,Long blogId) {
        //    List<Comment> commentsView = new ArrayList<>();
        List<Comment> parentComments = commentRepository.listParemtIdNull(blogId);
        combineChildren(parentComments);
        return parentComments;
    }

    /**
     * @return void
     * @Author zhouchb
     * @Param [comments](父级评论)
     * @Description: 取出顶级评论的子评论
     * @Date 8:43 2021/9/14
     */
    private void combineChildren(List<Comment> comments) {
        for (Comment comment : comments) {
            List<Comment> replays = comment.getReplyComments(); //获取父级评论的子评论
            for (Comment replay : replays) {
                //循环迭代，找出子代 ，存放在tempReplys
                recursively(replay);
            }
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setReplyComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
    }

    //存放迭代找出的所有子代评论的集合
    public List<Comment> tempReplys = new ArrayList<>();

    /**
     * @return void
     * @Author zhouchb
     * @Param [comment](二级评论)
     * @Description: 递归迭代
     * @Date 8:52 2021/9/14
     */
    private void recursively(Comment comment) {
        tempReplys.add(comment);//顶节点添加到临时存放集合
        if (comment.getReplyComments().size() > 0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size() > 0) {
                    recursively(reply);
                }
            }
        }
    }
}
